<?php

namespace App\Admin\Controllers;

use App\Models\AdminPermission;
use App\Models\AdminRole;
use App\Models\AdminUser;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;

class AdminUserController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
    	$userModel = config('admin.database.users_model');
        $grid = new Grid(new AdminUser());

        $grid->column('id', __('Id'));
        $grid->column('username', __('Username'));
        $grid->column('name', __('Name'));
        $grid->column('roles', '')->display(function ($roles) {
            $roles  = array_map(function ($role) {
                return "<span class='label label-success'>{$role['name']}</span>";
                }, $roles);

            return join('&nbsp;', $roles);
        });
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        $grid->actions(function($action) {
            $action->disableView();
        });
        
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->like('name', '名称');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$userModel = config('admin.database.users_model');
        $show = new Show($userModel::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        $show->field('username', __('Username'));
        $show->field('password', __('Password'));
        $show->field('name', __('Name'));
        $show->field('avatar', __('Avatar'));
        $show->field('remember_token', __('Remember token'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
    	$userModel = config('admin.database.users_model');
        $form = new Form(new AdminUser());

        $form->display('id', 'ID');
        $form->text('username', '用户名')
            ->creationRules(['required', "unique:admin_users"])
            ->updateRules(['required', "unique:admin_users,username,{{id}}"]);

        $form->text('name', trans('admin.name'))->rules('required');
        $form->image('avatar', trans('admin.avatar'));
        $form->password('password', trans('admin.password'))->rules('required|confirmed');
        $form->password('password_confirmation', trans('admin.password_confirmation'))->rules('required')
            ->default(function ($form) {
                return $form->model()->password;
            });

        $form->ignore(['password_confirmation']);

        $form->multipleSelect('roles', '角色')
            ->options(AdminRole::where('store_id', Admin::user()->store_id)->orWhere('id', '1')->get()->pluck('name', 'id'));

        
//        $form->multipleSelect('permissions', '权限')
//            ->options(AdminPermission::all()->pluck('name', 'id'));

        $form->hidden('store_id');
        $form->saving(function (Form $form) {
            if ($form->password && $form->model()->password != $form->password) {
                $form->password = bcrypt($form->password);
            }
            $admin = Auth::guard('admin')->user();
            $form->store_id = $admin->store_id;

        });
        
        $form->saved(function (Form $form) {
            
        });

        $form->tools(function ($tool) {
            $tool->disableView();
        });

        $form->footer(function ($foot) {
            $foot->disableViewCheck();
        });

        return $form;
    }
}
